import com.alibaba.fastjson.JSON;
import com.aliyun.odps.udf.UDF;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 解析商品title中的包装规格
 * 
 * @author asus
 *
 */
public class SpliderGoodsTitlePacking  extends UDF  {
	/*public static void main(String[] args) {

		String title = "jvr妮维雅男士矿物质洁面乳(泵瓶版)";
		String title1 = "50ml双飞人爽水";
		String title2 = "20袋装50ml双飞人爽水";
		String title3 = "宓月花和经泌套（2+2体验装）宓妍洁润慕斯乳";
		String title4 ="伊斯佳莹润滋养套装（33合1）—伊斯佳养颜液";
		String title5 = "5*50ml双飞人爽水";
		evaluate(title1);
	}*/

	public String evaluate(String title) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			StringBuffer sb = new StringBuffer();
			String reg1 = "";
			String reg = "(\\d+.装\\d+ml|\\d+.装|（.+装）|.合.+套装|\\(.合.\\)|\\(\\d+合\\d+\\)|（.合.）|（\\d+合\\d+）|体验装|\\d+.版|\\(.+版\\)|（.+版）|..版)" +
					"|\\d+\\*\\d+ml|\\d+ml|\\d+ML|\\(.+装\\)";
			Pattern pattern = Pattern.compile(reg);
			Matcher matcher = pattern.matcher(title);
			boolean find = matcher.find();
			System.out.println(find);
			//System.out.println(matcher.group());
			if (find) {
				// System.out.println(matcher.groupCount());
				System.out.println("包装：" + matcher.group(0));
				matcher.appendReplacement(sb, "");
				matcher.appendTail(sb);
				System.out.println("提取后title：" + sb.toString());
				map.put("packing", matcher.group(1));
				map.put("replaceTitle", sb.toString());
				return JSON.toJSONString(map);
			}
		}catch(Exception e) {
			map.put("errTitle", title);
			map.put("exception", e.getMessage());
			return JSON.toJSONString(map);
		}
		return JSON.toJSONString(map);
	}

}
